package models;

import java.util.UUID;

import javax.persistence.Entity;
import javax.persistence.Id;

import jiyici.Constant.ModelColumn;
import play.db.ebean.Model;

@Entity
public class User extends Model {

    private static final long serialVersionUID = 1916473007162202683L;

    @Id
    public Long id;
    
    public String email;

    public String userName;

    public String password;

    public String token;

    public static Model.Finder<Long, User> find = new Model.Finder<Long, User>(Long.class, User.class);

    public static User findByEmail(String email) {
        return find.where().eq(ModelColumn.USER_COLUMN_EMAIL, email).findUnique();
    }

    public static User register(String email, String userName, String password) {
        if (findByEmail(email) != null)
            return null;
        User user = new User();
        user.email = email;
        user.userName = userName;
        user.password = password;
        user.token = UUID.randomUUID().toString();
        user.save();
        return user;
    }

    public static User authenticateByEmailPassword(String email, String password) {
        return find.where().eq(ModelColumn.USER_COLUMN_EMAIL, email).eq(ModelColumn.USER_COLUMN_PASSWORD, password)
                .findUnique();
    }

    public static User authenticateByIdToken(Long id, String token) {
        return find.where().eq(ModelColumn.USER_COLUMN_ID, id).eq(ModelColumn.USER_COLUMN_TOKEN, token).findUnique();
    }
    
    public static void changePassword(User user, String oldPassword, String newPassword) {
        
    }
}
